package com.gdk.ksoft.assembly.utils.system;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by hel on 2015-01-08.
 * 时间操作辅助类
 */
public class TimeUtils {

    /**
     * 比较两个时间相隔的天数
     * @param calendar1
     * @param calendar2
     * @return
     */
    public static int computeTimeSpan(Calendar calendar1,Calendar calendar2){
        long timeSpan = Math.abs(calendar1.getTimeInMillis()-calendar2.getTimeInMillis());
        return (int)Math.ceil(timeSpan/(24*60*60*1000));
    }


    /**
     * 获取本地时间
     * @return
     * @throws Exception
     */
    public static Date getLocalTime() throws Exception{
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.add(Calendar.DAY_OF_MONTH,1);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int date = calendar.get(Calendar.DAY_OF_MONTH);
        return new SimpleDateFormat("yyy-MM-dd HH:mm:ss").parse(year+"-"+month+"-"+date);
    }

}
